package com.idrem.views.location;

import com.idrem.R;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LocationAct extends Activity implements LocationListener {
	private EditText longitude;
	private EditText dimensions;
	private LocationManager lm;
	private final String TAG = "GPSINFO";

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (lm != null) {
			// 重新获取GPS信息
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l, 1l,
					this);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (lm != null) {
			lm.removeUpdates(this);// 移除那个锅，也就是告诉GPS服务器不要发送GPS信息呢
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);
		Button btn = (Button) findViewById(R.id.btn_get);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				InitEditText();
				InitLocation();
			}
		});
		Button btn_send = (Button) findViewById(R.id.btn_send);
		btn_send.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "发送本机位置信息之服务器端", Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void InitLocation() {
		// 使用标准集合，让系统自动选择可用的最佳位置提供器，提供位置
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
		criteria.setAltitudeRequired(false);// 不要求海拔
		criteria.setBearingRequired(false);// 不要求方位
		criteria.setCostAllowed(true);// 允许有花费
		criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
		lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		// 从可用的位置提供器中，匹配以上标准的最佳提供器
		String provider = lm.getBestProvider(criteria, true);
		// 获得最后一次变化的位置
		Location location = lm.getLastKnownLocation(provider);

		updataGpsWidthLocation(location);
		// 首次进去获取GSP信息
		// 每隔一定的时间通知server去更新一下GPS信息
		// 这个时候屏幕上面应该会出现一个锅了
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l, 1l, this);
	}

	/*
	 * update gps information with location
	 */
	private void updataGpsWidthLocation(Location location) {
		if (location != null) {
			double lit = location.getLongitude();// 经度
			double dimen = location.getLatitude();// 纬度
			longitude.setText("" + lit);
			this.dimensions.setText("" + dimen);
		} else {
			longitude.setText("get longitude fail");
			dimensions.setText("get dimensions fail");
		}
	}

	private void InitEditText() {
		longitude = (EditText) this.findViewById(R.id.longitude);
		dimensions = (EditText) this.findViewById(R.id.latitude);
	}

	private void getLocation(Location location) {
		if (location != null) {
			double lit = location.getLongitude();// 经度
			double dimen = location.getLatitude();// 纬度
			longitude.setText("" + lit);
			this.dimensions.setText("" + dimen);
		} else {
			longitude.setText("get longitude fail");
			dimensions.setText("get dimensions fail");
		}

	}
	@Override
	public void onLocationChanged(Location location) {
		// 该函数为系统函数，每隔一定的时间便会自动进行调用
		// location change
		Log.e(TAG, "location change");
		updataGpsWidthLocation(location);
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		Log.e(TAG, "statusChanged");
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		Log.e(TAG, "providerenabled");
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		Log.e(TAG, "providerdisabled");
	}
}
